package cc.web.vaaking.common.core;

import cc.web.vaaking.common.LoginContext;
import cc.web.vaaking.entity.SysAppLog;
import cc.web.vaaking.entity.SysVacationRecord;
import cc.web.vaaking.services.SysAppLogService;
import cc.web.vaaking.services.SysVacationRecordService;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailUtil;

public class HRHandler extends EventHandler {
    SysVacationRecordService sysVacationRecordService = new SysVacationRecordService();
    SysAppLogService sysAppLogService = new SysAppLogService();

    public HRHandler(SysVacationRecord sysVacationRecord, boolean app) {
        super(sysVacationRecord, app);
    }

    @Override
    public void doBefore() {
    }

    @Override
    void process() {
        SysAppLog sysAppLog = new SysAppLog();
        sysAppLog.setUserId(LoginContext.getCurrentUser().getId());
        sysAppLog.setRecordId(sysVacationRecord.getId());
        sysAppLog.setOpComment(app ? "pass" : "reject");
        sysVacationRecord.setStatus(next());

        sysAppLogService.insertOne(sysAppLog);
        sysVacationRecordService.updateByPrimaryKeySelective(sysVacationRecord);
    }

    @Override
    public void doAfter() {
        String content = "您单号为 {} 的请假单《{}》已经被 {}。";
        if (app)
            content = StrUtil.format(content, sysVacationRecord.getId(),sysVacationRecord.getTitle(), "HR审核通过");
        else
            content = StrUtil.format(content, sysVacationRecord.getId(),sysVacationRecord.getTitle(), "HR拒绝");
        MailUtil.send(sysVacationRecord.getSysUser().getEmail(), "请假进度通知", content, false);
    }

    String next() {
        if (app) {
            return "1";
        } else {
            return "3";
        }
    }


}
